package com.it.you.base.io;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MyJframe extends JFrame {
    // 布局很简单，直接看swing菜鸟教程就可以，五分钟搞定
    public static void placeComponents(JPanel panel) {
        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        // 创建 JLabel
        JLabel resFileLabel = new JLabel("源文件目录:");
        resFileLabel.setBounds(10, 20, 130, 25);
        panel.add(resFileLabel);
        JTextField resFileText = new JTextField(20);
        resFileText.setBounds(160, 20, 200, 25);
        panel.add(resFileText);

        // 目标文件目录
        JLabel targetFileDir = new JLabel("目标文件目录:");
        targetFileDir.setBounds(10, 50, 130, 25);
        panel.add(targetFileDir);
        JTextField targetFileDirText = new JTextField(20);
        targetFileDirText.setBounds(160, 50, 200, 25);
        panel.add(targetFileDirText);

        // 目标文件名
        JLabel targetFileName = new JLabel("目标文件名:");
        targetFileName.setBounds(10, 80, 130, 25);
        panel.add(targetFileName);
        JTextField targetFileNameText = new JTextField(20);
        targetFileNameText.setBounds(160, 80, 200, 25);
        panel.add(targetFileNameText);

        // 目标文件大小
        JLabel targetFileSizeLabel = new JLabel("目标文件大小（kb）:");
        targetFileSizeLabel.setBounds(10, 110, 130, 25);
        panel.add(targetFileSizeLabel);
        JTextField targetFileSizeText = new JTextField(20);
        targetFileSizeText.setBounds(160, 110, 200, 25);
        panel.add(targetFileSizeText);

        // 开始分割
        JButton splitButton = new JButton("开始分割");
        splitButton.setBounds(10, 170, 100, 25);
        panel.add(splitButton);

        splitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String resFile = resFileText.getText();
                String targetFileDir = targetFileDirText.getText();
                String targetFileName = targetFileNameText.getText();
                String targetFileSize = targetFileSizeText.getText();
                try {
                    splitByFileReader(resFile, targetFileDir, targetFileName, Integer.valueOf(targetFileSize));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        // 创建 JFrame 实例
        JFrame frame = new JFrame("文件分割器");
        frame.setBounds(400, 180, 400, 240);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        // 添加面板
        frame.add(panel);
        MyJframe.placeComponents(panel);
        // 设置界面可见
        frame.setVisible(true);
    }


    /**
     * 文件字符流分割文件
     *
     * @throws IOException
     */
    private static void splitByFileReader(String resFile, String targetFileDir, String targetFileName, int targetFileSize) throws IOException {
        // 读取文件
        FileReader fileReader = new FileReader(resFile);
        FileWriter fileWriter = new FileWriter(targetFileDir + "/" + targetFileName + ".txt");
        // 一次读1024个字符
        char[] buf = new char[1024];
        int count = 0;
        int id = 1;
        while (fileReader.read(buf) != -1) {
            // 读取targetFileSize次之后，开始向第二个txt注入
            if (count != 0 && count % targetFileSize == 0) {
                fileWriter.close();
                fileWriter = new FileWriter(targetFileDir + "/" + targetFileName + id + ".txt");
                id++;
            }
            fileWriter.write(buf);
            count++;
        }
        fileReader.close();
        fileWriter.close();
    }

}

